#! /bin/bash

mkcd() {
    mkdir -p "$1"
    cd "$1"
}

cdl() {
    cd "$1"
    ls --color=auto
}

mdtopdfview() {
    cat "$1" | pandoc -V 'mainfont:Hack-Regular.ttf' --pdf-engine=lualatex -f markdown -t pdf -V 'geometry:margin=50px' | zathura -
}

hide() {
    lvlps1=$(((lvlps1 + 1) % 2))
    if [ $lvlps1 -eq 0 ]; then PS1='\[\033[01;35m\][\h@\u]\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\n$ '
    else PS1=': '; fi
}

screenrecord() {
    name="vid-$(date +%Y-%m-%d_%H-%M)".mp4

    ffmpeg \
       -f x11grab \
       -rtbufsize 1024M \
       -s 1920x1080 \
       -framerate 60 \
       -probesize 100M \
       -draw_mouse 1 \
       -i $DISPLAY \
       -c:v libx264 \
       -r 60 \
       -preset ultrafast \
       -tune zerolatency \
       -crf 23 \
       -pix_fmt yuv420p "$name"
}

lowername() {
    FLIST=$HOME/f_list.txt
    find . -type f > $FLIST

    less $FLIST

    lines=$(cat $FLIST | wc -l)

    for i in $(seq 1 $lines); do
        name=$(cat $FLIST | head -$i | tail -1)
        newname=$(echo "$name" | tr '[:upper:]' '[:lower:]')

        oldfolder=$(dirname "$name")
        newfolder=$(dirname "$newname")

        bname=$(basename "$name")
        bnewname=$(basename "$newname")

        if [ "$name" != "$oldname" ]; then
            flgfolder=false
            [ "$oldfolder" != "$newfolder" ] && flgfolder=true

            $flgfolder && mkdir -p "$newfolder"

            if [ "$bname" != "$bnewname" ]; then
                mv "$name" "$newname" && rm -rfv $name
            else
                if $flgfolder; then mv "$oldfolder/$bname" "$newfolder/$bname"; fi
            fi
            echo $newname
        fi

        echo -ne "\e[0;31m$i\e[0;0m of \e[0;31m$lines\e[0;0m\n"
    done

    find . -type d -empty -print -delete
    rm $FLIST
}

rf() {
    cmd="doas rfkill"
    while [ -n "$1" ]; do
        case $1 in
            "u")  cmd="$cmd unblock"   ;;
            "b")  cmd="$cmd block"     ;;
            "wi") cmd="$cmd wifi"      ;;
            "bl") cmd="$cmd bluetooth" ;;
        esac
        shift
    done
    $cmd
}

elog() {
    case $1 in
        ""|"-?"|"-h")
            echo "USAGE: elog OPTION"
            echo -ne "-? -h\tshow this message\n"
            echo -ne "-f\tshow emerge-fetch.log\n"
            echo -ne "-e\tshow emerge.log\n\n"
        ;;

        "-e") doas tail -f /var/log/emerge.log ;;
        "-f") doas tail -f /var/log/emerge-fetch.log ;;
    esac
}

show_images() {
    feh -FZd -S filename --force-aliasing -f $1
    dragon -a -x $(cat $1)
}

um() {
    udisksctl unmount -b $1 && doas hdparm -Y ${1::(-1)}
}

winekill() {
    kill -9 $(ps aux | grep -i 'wine\|C:\windows' | cut -d ' ' -f 7)
}

gdl() {
    go doc "$1" | less
}
